package com.lehezn.opensource.appkit.ControlModule;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;

import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.lehezn.opensource.appkit.R;

/**
 * Created by es-ll on 17/10/22.
 */

public class SettingActivity extends GosControlModuleBaseActivity {
    private Spinner spMeterInterval, spMeterSumPoll, spPowerChangeValue, spGosWaterPoll;
    private Switch switchRelay;
    private EditText e_jian,e_feng,e_ping,e_gu,e_heart,e_water,e_gos;
    double priceOfJian,priceOfFeng,priceOfPing,priceOfGu,priceOfWater,priceOfGos,priceOfHeater;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a_setting);
        initDevice();
        initPrice();

        initView();
        setActionBar(true, true, "更多设置");
        addListener();

    }

    private void initDevice() {
        Intent intent = getIntent();
        device = (GizWifiDevice) intent.getParcelableExtra("GizWifiDevice");
    }


    private void addListener() {

        spMeterInterval.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                sendJson(KEY_METER_INTERVAL, UtilOfSetting.convertMeterIntervalSpinnerPosToInteger(i));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        spMeterSumPoll.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                sendJson(KEY_METER_SUM_POLL, UtilOfSetting.convertMeterSumPollSpinnerPosToInteger(i));

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        spPowerChangeValue.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                sendJson(KEY_POWER_CHANGE_VALVE, UtilOfSetting.convertMeterPowerChangeSpinnerPosToInteger(i));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        spGosWaterPoll.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                sendJson(KEY_GAS_WATER_POLL, UtilOfSetting.convertGosWaterPollSpinnerPosToInteger(i));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        switchRelay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                sendJson(KEY_RELAY, b);
            }
        });


    }

    private void initView() {
        spMeterInterval = (Spinner) findViewById(R.id.sp_meter_interval);
        spMeterSumPoll = (Spinner) findViewById(R.id.sp_meter_sum_poll);
        spPowerChangeValue = (Spinner) findViewById(R.id.sp_power_change_value);
        spGosWaterPoll = (Spinner) findViewById(R.id.sp_gos_water_poll);

        switchRelay = (Switch) findViewById(R.id.switch_relay);
        switchRelay.setChecked(data_relay);

        spMeterInterval.setSelection(UtilOfSetting.convertMeterIntervalValueToSpinnerPos(data_meter_interval));
        spMeterSumPoll.setSelection(UtilOfSetting.convertMeterSumPollValueToSpinnerPos(data_meter_sum_Poll));
        spPowerChangeValue.setSelection(UtilOfSetting.convertMeterPowerChangeValueToSpinnerPos(data_power_change_valve));
        spGosWaterPoll.setSelection(UtilOfSetting.convertGasWaterPollValueToSpinnerPos(data_gas_water_poll));


        e_jian= (EditText) findViewById(R.id.e_jian);
        e_feng= (EditText) findViewById(R.id.e_feng);
        e_ping= (EditText) findViewById(R.id.e_ping);
        e_gu= (EditText) findViewById(R.id.e_gu);
        e_water= (EditText) findViewById(R.id.et_water);
        e_gos= (EditText) findViewById(R.id.et_gos);
        e_heart= (EditText) findViewById(R.id.et_heart);

        e_jian.setText(priceOfJian+"");
        e_feng.setText(priceOfFeng+"");
        e_ping.setText(priceOfPing+"");
        e_gu.setText(priceOfGu+"");
        e_water.setText(priceOfWater+"");
        e_gos.setText(priceOfGos+"");
        e_heart.setText(priceOfHeater+"");




    }

    private void initPrice() {
        try {


            priceOfJian = Double.parseDouble(spf.getString(Util.PRICE_JIAN, "0.3"));
            priceOfFeng = Double.parseDouble(spf.getString(Util.PRICE_FENG, "0.4"));
            priceOfPing = Double.parseDouble(spf.getString(Util.PRICE_PING, "0.5"));
            priceOfGu = Double.parseDouble(spf.getString(Util.PRICE_GU, "0.2"));
            priceOfWater = Double.parseDouble(spf.getString(Util.PRICE_WATER, "0.5"));
            priceOfGos = Double.parseDouble(spf.getString(Util.PRICE_GOS, "0.5"));
            priceOfHeater = Double.parseDouble(spf.getString(Util.PRICE_HEART, "0.5"));
        }catch (Exception e){
            e.printStackTrace();
        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.save, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.saves:

                spf.edit().putString(Util.PRICE_JIAN, e_jian.getText().toString()).commit();
                spf.edit().putString(Util.PRICE_FENG, e_feng.getText().toString()).commit();
                spf.edit().putString(Util.PRICE_PING, e_ping.getText().toString()).commit();
                spf.edit().putString(Util.PRICE_GU, e_gu.getText().toString()).commit();
                spf.edit().putString(Util.PRICE_WATER, e_water.getText().toString()).commit();
                spf.edit().putString(Util.PRICE_GOS, e_gos.getText().toString()).commit();
                spf.edit().putString(Util.PRICE_HEART, e_heart.getText().toString()).commit();



                finish();



                break;

            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}
